import os.path

import numpy as np
import time


def picked_filename_to_csv_name(s):
    return s.split('_')[1].split('.')[0] + '.csv'


def pickup_workloads(filedir):
    filenames = os.listdir(filedir)
    write_number_inloads = []
    read_ratio_inruns = []
    num_of_keys_in_alls = []

    for filename in filenames:
        filepath = os.path.join(filedir, filename)
        with open(filepath) as f:
            lines = f.readlines()

            for line in lines:
                if "write_number_inload" in line:
                    write_number_inload = int(line.split('-')[1][:-1])
                    write_number_inloads.append(write_number_inload)
                if "read_ratio_inrun" in line:
                    read_ratio_inrun = float(line.split('-')[1][:-1])
                    read_ratio_inruns.append(read_ratio_inrun)
                if "num of keys in all" in line:
                    num_of_keys_in_all = int(line.split('-')[1][:-1])
                    num_of_keys_in_alls.append(num_of_keys_in_all)  # 注意每个都是列表才行
    assert len(read_ratio_inruns) == len(write_number_inloads)

    read_ratio_inruns = np.array(read_ratio_inruns)
    write_number_inloads = np.array(write_number_inloads)
    num_of_keys_in_alls = np.array(num_of_keys_in_alls)  # 不能忘，可能不同python版本就不一样了

    rd_pct_threshold0 = 0.8
    rd_pct_threshold1 = 0.9
    write_num_load_threshold0 = 5000000
    write_num_load_threshold1 = 80000000
    all_num_threshold0 = 150000000
    all_num_threshold1 = 950000000

    picked_reads_more = np.where(read_ratio_inruns > rd_pct_threshold0)
    picked_reads_less = np.where(read_ratio_inruns < rd_pct_threshold1)
    picked_writes_more = np.where(write_number_inloads > write_num_load_threshold0)
    picked_writes_less = np.where(write_number_inloads < write_num_load_threshold1)
    picked_all_more = np.where(num_of_keys_in_alls > all_num_threshold0)
    picked_all_less = np.where(num_of_keys_in_alls < all_num_threshold1)

    print(picked_reads_more, picked_reads_less, picked_writes_more, picked_writes_less, picked_all_more, picked_all_less)

    picked_set = set(picked_reads_more[0]) & set(picked_reads_less[0]) & set(picked_writes_more[0]) & set(picked_writes_less[0]) & set(picked_all_more[0]) & set(picked_all_less[0])
    for serial in picked_set:
        print("filename: %s, write_number_inloads: %d, read_ratio_inruns: %.2f, num_of_keys_in_alls: %d" % (filenames[serial], write_number_inloads[serial], read_ratio_inruns[serial], num_of_keys_in_alls[serial]))

    picked_filenames = np.array(filenames)[list(picked_set)]

    with open("picked_filenames_rdPct%.2f_%.2f__writeNum%.0fM_%.0fM_allNum%.0fM_%.0fM.txt" % (rd_pct_threshold0, rd_pct_threshold1, write_num_load_threshold0/1000000, write_num_load_threshold1/1000000, all_num_threshold0/1000000, all_num_threshold1/1000000), "w") as f:
        for picked_filename in picked_filenames:
            f.write(picked_filename_to_csv_name(picked_filename) + ' ')


if __name__ == "__main__":
    st = time.time()

    file_dir = '../evaluation_analyse/'
    pickup_workloads(file_dir)

    elps = time.time() - st
    print('run for %.1f s' % elps)
